using System;
using System.Web.UI;
using Portal.Data.Entity;

namespace Portal.Data.Webparts
{
    public class WebpartControl : UserControl, IWebpartControl
    {
        #region IWebpartControl Members
        private Webpart _info;
        private WebpartConfig _config;

        public virtual WebpartConfig NewConfig
        {
            get
            {
                return new WebpartConfig();
            }
        }

        public Webpart Info
        {
            get
            {
                return _info;
            }
            set
            {
                _info = value;
                var config = Info.GetConfig();
                if (config == null)
                {
                    _config = NewConfig;
                    _info.SetConfig(_config);
                }
                else
                {
                    _config = config;
                }
            }
        }

        public virtual WebpartConfig Config
        {
            get
            {
                if (_config == null)
                {
                    if (_info != null)
                    {
                        _config = _info.GetConfig();
                    }
                    if (_config == null)
                    {
                        _config = NewConfig;
                    }
                }
                return _config;
            }
        }

        public void OnAfterContain(IContainer container)
        {
        }
        public PageViewMode ViewMode { get; set; }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Request.Url.LocalPath.IndexOf("/admin", StringComparison.OrdinalIgnoreCase) >= 0)
                return;
            base.Render(writer);
        }
        #endregion
    }
}
